/*
 * PConfiguracionFlujosFormulada.java
 *
 * Created on 16 de marzo de 2007, 08:14 AM
 */

package ig.metrica.ui.confFlujos.formulada;

import ig.metrica.ui.interfaces.comunes.IMetodosPantallas;
import ig.metrica.ui.modelo.WizzardModelo;
import ig.util.Matematica;
import ig.util.MyTableModel;
import ig.util.NoContinuaException;
import java.awt.Component;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Hashtable;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author  jsanchez
 */
public class PConfiguracionFlujosFormulada extends javax.swing.JPanel implements  IMetodosPantallas{
    
    /**
     * Creates new form PConfiguracionFlujosFormulada
     */
    private String nombreModelo=null;
    private MConfFormuladas modelo=null;
    private WizzardModelo wizzard=null;
    public PConfiguracionFlujosFormulada(WizzardModelo wizzard) throws NoContinuaException {
        initComponents();
        this.wizzard=wizzard;
        modelo=new MConfFormuladas(this);
        enabled(false);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        PMenu = new javax.swing.JPopupMenu();
        ValidarFormula = new javax.swing.JMenuItem();
        EvaluarFormula = new javax.swing.JMenuItem();
        PMenuInsert = new javax.swing.JPopupMenu();
        MItemInsertar = new javax.swing.JMenuItem();
        GrupoFormula = new javax.swing.ButtonGroup();
        PMenuNew = new javax.swing.JPopupMenu();
        AddFormula = new javax.swing.JMenuItem();
        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        T_Productos = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        TableFormulas = new javax.swing.JTable();
        jScrollPane4 = new javax.swing.JScrollPane();
        TACampos = new javax.swing.JTextArea();
        RCapital = new javax.swing.JRadioButton();
        RFecha = new javax.swing.JRadioButton();
        RIFijo = new javax.swing.JRadioButton();
        RIVariable = new javax.swing.JRadioButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        TA_Formula = new javax.swing.JTextArea();

        ValidarFormula.setText(org.openide.util.NbBundle.getMessage(PConfiguracionFlujosFormulada.class, "PConfiguracionFlujosFormulada.ValidarFormula.text")); // NOI18N
        ValidarFormula.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ValidarFormulaActionPerformed(evt);
            }
        });

        PMenu.add(ValidarFormula);

        EvaluarFormula.setText(org.openide.util.NbBundle.getMessage(PConfiguracionFlujosFormulada.class, "PConfiguracionFlujosFormulada.EvaluarFormula.text")); // NOI18N
        EvaluarFormula.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EvaluarFormulaActionPerformed(evt);
            }
        });

        PMenu.add(EvaluarFormula);

        MItemInsertar.setText(org.openide.util.NbBundle.getMessage(PConfiguracionFlujosFormulada.class, "PConfiguracionFlujosFormulada.MItemInsertar.text")); // NOI18N
        MItemInsertar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MItemInsertarActionPerformed(evt);
            }
        });

        PMenuInsert.add(MItemInsertar);

        AddFormula.setText(org.openide.util.NbBundle.getMessage(PConfiguracionFlujosFormulada.class, "PConfiguracionFlujosFormulada.AddFormula.text")); // NOI18N
        AddFormula.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddFormulaActionPerformed(evt);
            }
        });

        PMenuNew.add(AddFormula);

        jSplitPane1.setDividerLocation(310);
        jSplitPane1.setDividerSize(6);
        jSplitPane1.setOneTouchExpandable(true);
        T_Productos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        T_Productos.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                T_ProductosKeyReleased(evt);
            }
        });
        T_Productos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                T_ProductosMouseReleased(evt);
            }
        });

        jScrollPane1.setViewportView(T_Productos);

        jSplitPane1.setLeftComponent(jScrollPane1);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jScrollPane3.setComponentPopupMenu(PMenuNew);
        TableFormulas.setComponentPopupMenu(PMenuNew);
        TableFormulas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {}
            },
            new String [] {

            }
        ));
        TableFormulas.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                TableFormulasKeyReleased(evt);
            }
        });
        TableFormulas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                TableFormulasMouseReleased(evt);
            }
        });

        jScrollPane3.setViewportView(TableFormulas);

        jScrollPane4.setBorder(null);
        TACampos.setBackground(javax.swing.UIManager.getDefaults().getColor("control"));
        TACampos.setColumns(20);
        TACampos.setComponentPopupMenu(PMenuInsert);
        TACampos.setFont(new java.awt.Font("Dialog", 1, 11));
        TACampos.setRows(2);
        TACampos.setText(org.openide.util.NbBundle.getMessage(PConfiguracionFlujosFormulada.class, "PConfiguracionFlujosFormulada.TACampos.text")); // NOI18N
        TACampos.setToolTipText(org.openide.util.NbBundle.getMessage(PConfiguracionFlujosFormulada.class, "PConfiguracionFlujosFormulada.TACampos.toolTipText")); // NOI18N
        TACampos.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        jScrollPane4.setViewportView(TACampos);

        GrupoFormula.add(RCapital);
        RCapital.setSelected(true);
        RCapital.setText(org.openide.util.NbBundle.getMessage(PConfiguracionFlujosFormulada.class, "PConfiguracionFlujosFormulada.RCapital.text")); // NOI18N
        RCapital.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        RCapital.setMargin(new java.awt.Insets(0, 0, 0, 0));
        RCapital.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RCapitalActionPerformed(evt);
            }
        });

        GrupoFormula.add(RFecha);
        RFecha.setText(org.openide.util.NbBundle.getMessage(PConfiguracionFlujosFormulada.class, "PConfiguracionFlujosFormulada.RFecha.text")); // NOI18N
        RFecha.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        RFecha.setMargin(new java.awt.Insets(0, 0, 0, 0));
        RFecha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RFechaActionPerformed(evt);
            }
        });

        GrupoFormula.add(RIFijo);
        RIFijo.setText(org.openide.util.NbBundle.getMessage(PConfiguracionFlujosFormulada.class, "PConfiguracionFlujosFormulada.RIFijo.text")); // NOI18N
        RIFijo.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        RIFijo.setMargin(new java.awt.Insets(0, 0, 0, 0));
        RIFijo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RIFijoActionPerformed(evt);
            }
        });

        GrupoFormula.add(RIVariable);
        RIVariable.setText(org.openide.util.NbBundle.getMessage(PConfiguracionFlujosFormulada.class, "PConfiguracionFlujosFormulada.RIVariable.text")); // NOI18N
        RIVariable.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        RIVariable.setMargin(new java.awt.Insets(0, 0, 0, 0));
        RIVariable.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RIVariableActionPerformed(evt);
            }
        });

        TA_Formula.setColumns(20);
        TA_Formula.setComponentPopupMenu(PMenu);
        TA_Formula.setFont(new java.awt.Font("Dialog", 1, 14));
        TA_Formula.setRows(5);
        TA_Formula.setText(org.openide.util.NbBundle.getMessage(PConfiguracionFlujosFormulada.class, "PConfiguracionFlujosFormulada.TA_Formula.text")); // NOI18N
        TA_Formula.setToolTipText(org.openide.util.NbBundle.getMessage(PConfiguracionFlujosFormulada.class, "PConfiguracionFlujosFormulada.TA_Formula.toolTipText")); // NOI18N
        TA_Formula.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                TA_FormulaMouseMoved(evt);
            }
        });
        TA_Formula.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                TA_FormulaKeyReleased(evt);
            }
        });
        TA_Formula.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                TA_FormulaMouseClicked(evt);
            }
        });

        jScrollPane2.setViewportView(TA_Formula);

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 334, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 334, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 334, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel1Layout.createSequentialGroup()
                        .add(RCapital)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(RFecha)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(RIFijo)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(RIVariable)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 103, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 36, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(RCapital)
                    .add(RFecha)
                    .add(RIFijo)
                    .add(RIVariable))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)
                .addContainerGap())
        );
        jSplitPane1.setRightComponent(jPanel1);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jSplitPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 652, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jSplitPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 344, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    
    private void AddFormulaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddFormulaActionPerformed
//        JOptionPane.showInputDialog("Aqui se adiciona una nueva fila en la tabla");
        Object obj=this.createNew(1);
        if(obj!=null && jScrollPane3.isEnabled()){
            this.CTDO_Filtrado.add(obj);
            joinCollectiones();
            setDataTableFormulas(this.CTDO_Filtrado);
        }
    }//GEN-LAST:event_AddFormulaActionPerformed
    
    private void TableFormulasMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TableFormulasMouseReleased
        eventoTablaFormula();
    }//GEN-LAST:event_TableFormulasMouseReleased
    
    private void eventoTablaFormula(){
        int row=TableFormulas.getSelectedRow();
        int col=TableFormulas.getSelectedColumn();
        if(col==1){
            Boolean ok=(Boolean)TableFormulas.getModel().getValueAt(row,col);
            ConFlujosFormuladosDTO DTOTabla=((ConFlujosFormuladosDTO)TableFormulas.getModel().getValueAt(row,0));
            DTOTabla.setActivo(DTOTabla.getActivo().equals(1)?0:1);
            for (Object elem1 : this.CDTO) {
                if(     DTOTabla.getProducto().trim().equals(((ConFlujosFormuladosDTO)elem1).getProducto()) &&
                        DTOTabla.getIdModleo().equals(((ConFlujosFormuladosDTO)elem1).getIdModleo()) &&
                        DTOTabla.getId().equals(((ConFlujosFormuladosDTO)elem1).getId())
                        ){
                    ((ConFlujosFormuladosDTO)elem1).setFormulaCapital(DTOTabla.getFormulaCapital());
                    ((ConFlujosFormuladosDTO)elem1).setFormulaFecha(DTOTabla.getFormulaFecha());
                    ((ConFlujosFormuladosDTO)elem1).setFormulaInteresFijo(DTOTabla.getFormulaInteresFijo());
                    ((ConFlujosFormuladosDTO)elem1).setFormulaInteresVariable(DTOTabla.getFormulaInteresVariable());
                    ((ConFlujosFormuladosDTO)elem1).setActivo(DTOTabla.getActivo());
                    ((ConFlujosFormuladosDTO)elem1).setEstadoRegistro(DTOTabla.getEstadoRegistro()!=null && !DTOTabla.getEstadoRegistro().equals(1) ? 2 : 1);
                    break;
                }
            }
        }else if(col==0)
            setConfiguracioFormulacion((ConFlujosFormuladosDTO)TableFormulas.getModel().getValueAt(row,col));
    }
    
    private void TableFormulasKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_TableFormulasKeyReleased
        eventoTablaFormula();
    }//GEN-LAST:event_TableFormulasKeyReleased
    
    private void RIVariableActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RIVariableActionPerformed
        setConfiguracioFormulacion(dto);
    }//GEN-LAST:event_RIVariableActionPerformed
    
    private void RIFijoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RIFijoActionPerformed
        setConfiguracioFormulacion(dto);
    }//GEN-LAST:event_RIFijoActionPerformed
    
    private void RFechaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RFechaActionPerformed
        setConfiguracioFormulacion(dto);
    }//GEN-LAST:event_RFechaActionPerformed
    
    private void RCapitalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RCapitalActionPerformed
        setConfiguracioFormulacion(dto);
    }//GEN-LAST:event_RCapitalActionPerformed
    
    private synchronized void joinCollectiones(){
        for (Object elem : CTDO_Filtrado) {
            boolean add=true;
            for (Object elem1 : this.CDTO) {
                if(     ((ConFlujosFormuladosDTO)elem).getProducto().trim().equals(((ConFlujosFormuladosDTO)elem1).getProducto()) &&
                        ((ConFlujosFormuladosDTO)elem).getIdModleo().equals(((ConFlujosFormuladosDTO)elem1).getIdModleo()) &&
                        ((ConFlujosFormuladosDTO)elem).getId().equals(((ConFlujosFormuladosDTO)elem1).getId())
                        ){
                    ((ConFlujosFormuladosDTO)elem1).setFormulaCapital(((ConFlujosFormuladosDTO)elem).getFormulaCapital());
                    ((ConFlujosFormuladosDTO)elem1).setFormulaFecha(((ConFlujosFormuladosDTO)elem).getFormulaFecha());
                    ((ConFlujosFormuladosDTO)elem1).setFormulaInteresFijo(((ConFlujosFormuladosDTO)elem).getFormulaInteresFijo());
                    ((ConFlujosFormuladosDTO)elem1).setFormulaInteresVariable(((ConFlujosFormuladosDTO)elem).getFormulaInteresVariable());
                    add=false;
                    break;
                }
            }
            if(add)
                CDTO.add((ConFlujosFormuladosDTO)elem);
        }
    }
    
    private synchronized void findConfiguracion(){
        dto=new ConFlujosFormuladosDTO();
        dto=null;
        this.TA_Formula.setText("");
//        this.CBox_Formular.setSelected(false);
        int row=T_Productos.getSelectedRow();
        int col=T_Productos.getSelectedColumn();
        if(row>-1 && col>-1)
            dataActual=((MyTableModel)T_Productos.getModel()).getValueAt(row,col);
        
        CTDO_Filtrado=new ArrayList();
        if(CDTO!=null && dataActual!=null)
            for (ConFlujosFormuladosDTO obj : CDTO) {
                if(obj.getProducto().trim().equals(dataActual.toString().trim())){
                    CTDO_Filtrado.add(obj);
//                    setConfiguracioFormulacion(obj);
                }
            }
            this.setDataTableFormulas(CTDO_Filtrado);
    }
    
    private void setDataTableFormulas(Collection CDTOFiltrado){
        String[] col={"Formulas","Activa"};
        boolean[] edt={false,true};
        Object[][] data=new Object[CDTOFiltrado.size()][2];
        int i=0;
        for (Object elem : CDTOFiltrado) {
            data[i][0]=elem;
            data[i][1]=((ConFlujosFormuladosDTO)elem).getActivo().equals(1)?true:false;
            i++;
        }
        this.TableFormulas.setModel(new MyTableModel(data,col,edt));
        TableFormulas.getTableHeader().setReorderingAllowed(false);
        this.TableFormulas.updateUI();
    }
    private void setConfiguracioFormulacion(ConFlujosFormuladosDTO obj){
        if(obj!=null){
            TA_Formula.setEnabled(true);
            dto=obj;
            if(this.RCapital.isSelected())
                this.TA_Formula.setText(obj.getFormulaCapital());
            else if(this.RFecha.isSelected())
                this.TA_Formula.setText(obj.getFormulaFecha());
            else if(this.RIFijo.isSelected())
                this.TA_Formula.setText(obj.getFormulaInteresFijo());
            else if(this.RIVariable.isSelected())
                this.TA_Formula.setText(obj.getFormulaInteresVariable());
        }else{
            TA_Formula.setEnabled(false);
        }
    }
    private void setConfiguracioFormulacion(String  obj){
        dto.setEstadoRegistro(dto.getEstadoRegistro()!=null && !dto.getEstadoRegistro().equals(1) ? 2 : 1);
        if(this.RCapital.isSelected())
            this.dto.setFormulaCapital(obj);
        else if(this.RFecha.isSelected())
            this.dto.setFormulaFecha(obj);
        else if(this.RIFijo.isSelected())
            this.dto.setFormulaInteresFijo(obj);
        else if(this.RIVariable.isSelected())
            this.dto.setFormulaInteresVariable(obj);
    }
    
    public JInternalFrame getInternalFrame() {
        return null;
    }
    
    public void validar() throws NoContinuaException {
    }
    
    public void guardar() throws NoContinuaException {
        modelo.guardar();
        this.buscar();
    }
    public void buscar() throws NoContinuaException {
        
        String[] colum={"Productos"};
        boolean[] edit={false};
        
        CDTO=this.modelo.buscar();
        
        Object[][] data=new Object[wizzard.getRelacionProductosFactores().size()][1];
        int i=0;
        
        ArrayList factorestemp = new ArrayList(wizzard.getRelacionProductosFactores().keySet());
        Collections.sort(factorestemp);
        
        for (Object elem : factorestemp) {
            Object llave = factorestemp.get(i);
            String llave1= null;
            String tmp= null;
            try {
                tmp=llave.toString();
                llave1=(String) wizzard.getRelacionEspeciesFactoresPantalla().get(tmp);
            } catch (Exception e) {
                e.printStackTrace();
                throw new NoContinuaException(e);
            }
            if(llave1!=null)
                data[i++][0]=llave1;
            else// jsanchez
                data[i++][0]=llave;
        }
        
        this.T_Productos.setModel(new MyTableModel(data,colum,edit));
        T_Productos.getTableHeader().setReorderingAllowed(false);
        this.T_Productos.updateUI();        
        this.findConfiguracion();
        
    }
    
    public void modificar() throws NoContinuaException {
    }
    
    public void nuevo() throws NoContinuaException {
    }
    
    public void eliminar() throws NoContinuaException {
    }
    
    public void salir() throws NoContinuaException {
    }
    
    public void imprimir() throws NoContinuaException {
    }
    
    public void cancelar() throws NoContinuaException {
    }
    
    public Component[] getControles() {
        return null;
    }
    
    public void setFrame(JFrame frame) {
    }
    
    public JFrame getFrame() {
        return null;
    }
    
    public void setEnabledAll(boolean b) {
        this.enabled(b);
    }
    
    public void setNombreModelo(String string)throws Exception {
        this.nombreModelo=string;
        this.modelo.setNombreModelo(string);
        this.buscar();
    }
    
    public Collection getData(){
        return this.CDTO;
    }
    
    
    private void MItemInsertarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MItemInsertarActionPerformed
        TA_Formula.insert(" "+this.TACampos.getSelectedText()+" ",TA_Formula.getSelectionEnd());
        this.setConfiguracioFormulacion(TA_Formula.getText());
    }//GEN-LAST:event_MItemInsertarActionPerformed
    
    private void TA_FormulaMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TA_FormulaMouseMoved
        formularActual=TA_Formula.getText();
    }//GEN-LAST:event_TA_FormulaMouseMoved
    
    private void T_ProductosKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_T_ProductosKeyReleased
        this.findConfiguracion();
    }//GEN-LAST:event_T_ProductosKeyReleased
    
    private void TA_FormulaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TA_FormulaMouseClicked
        formularActual=TA_Formula.getText();
    }//GEN-LAST:event_TA_FormulaMouseClicked
    
    private void EvaluarFormulaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EvaluarFormulaActionPerformed
        try {
            JOptionPane.showMessageDialog(this,"El resultado de la formula es:"+Matematica.evaluaExpresion(this.formularActual.trim()));
        } catch (Exception e) {
//            e.printStackTrace();
            JOptionPane.showMessageDialog(this,"Error al evaluar la formula"+e.toString(),"Mensaje",JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_EvaluarFormulaActionPerformed
    
    private void ValidarFormulaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ValidarFormulaActionPerformed
        try {
            boolean resultado=Matematica.validaExpresion(this.formularActual.trim());
            if(resultado && !this.formularActual.trim().equals(""))
                JOptionPane.showMessageDialog(this,"La formular esta bien formada.");
            else
                JOptionPane.showMessageDialog(this,"La formular esta mal formada.","Mensaje",JOptionPane.ERROR_MESSAGE);
        } catch (Exception e) {
//            e.printStackTrace();
            JOptionPane.showMessageDialog(this,e.toString(),"Mensaje",JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_ValidarFormulaActionPerformed
    
    private void TA_FormulaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_TA_FormulaKeyReleased
        formularActual=TA_Formula.getText();
        if(dataActual!=null){
            if(dto!=null){
                this.setConfiguracioFormulacion(TA_Formula.getText());
                dto.setEstadoRegistro(dto.getEstadoRegistro()!=null && !dto.getEstadoRegistro().equals(1) ? 2 : 1);
            }/*else{
                dto=this.createNew(1);
                if(CDTO==null)
                    CDTO=new ArrayList();
                this.CDTO.add(dto);
            }*/
        }
    }//GEN-LAST:event_TA_FormulaKeyReleased
    
    
    private Integer getSiguienteId(){
        Integer i=1;
        boolean seguir=true;
        while(seguir && CTDO_Filtrado!=null){
            seguir=false;
            for (Object elem : CTDO_Filtrado) {
                if(((ConFlujosFormuladosDTO)elem).getId().equals(i)){
                    seguir=true;
                    i++;
                    break;
                }
            }
        }
        return i;
    }
    
    private ConFlujosFormuladosDTO createNew(int activo){
        ConFlujosFormuladosDTO _dto=null;
        if(dataActual!=null){
            _dto=new ConFlujosFormuladosDTO(
                    getSiguienteId(),
                    dataActual.toString().trim(),
                    this.modelo.getIdModelo(),
                    activo,
                    RCapital.isSelected()?this.TA_Formula.getText():"",
                    RFecha.isSelected()?this.TA_Formula.getText():"",
                    RIFijo.isSelected()?this.TA_Formula.getText():"",
                    RIVariable.isSelected()?this.TA_Formula.getText():"");
            _dto.setEstadoRegistro(1);
            _dto.setIdModleo(this.modelo.getIdModelo());
        }
        return _dto;
    }
    private void enabled(boolean b){
        this.TA_Formula.setEnabled(b);
        this.RCapital.setEnabled(b);
        this.RFecha.setEnabled(b);
        this.RIFijo.setEnabled(b);
        this.RIVariable.setEnabled(b);
        this.TACampos.setEnabled(b);
        ((javax.swing.JViewport)this.jScrollPane3.getComponents()[0]).getComponents()[0].setEnabled(b);
        this.jScrollPane3.setEnabled(b);
    }
    private void T_ProductosMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_T_ProductosMouseReleased
        this.findConfiguracion();
    }//GEN-LAST:event_T_ProductosMouseReleased
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem AddFormula;
    private javax.swing.JMenuItem EvaluarFormula;
    private javax.swing.ButtonGroup GrupoFormula;
    private javax.swing.JMenuItem MItemInsertar;
    private javax.swing.JPopupMenu PMenu;
    private javax.swing.JPopupMenu PMenuInsert;
    private javax.swing.JPopupMenu PMenuNew;
    private javax.swing.JRadioButton RCapital;
    private javax.swing.JRadioButton RFecha;
    private javax.swing.JRadioButton RIFijo;
    private javax.swing.JRadioButton RIVariable;
    private javax.swing.JTextArea TACampos;
    private javax.swing.JTextArea TA_Formula;
    private javax.swing.JTable T_Productos;
    private javax.swing.JTable TableFormulas;
    private javax.swing.JMenuItem ValidarFormula;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JSplitPane jSplitPane1;
    // End of variables declaration//GEN-END:variables
    
    private String formularActual=null;
    private Collection<ConFlujosFormuladosDTO> CDTO=null;
    private Object dataActual=null;
    private ConFlujosFormuladosDTO dto=null;
    private Collection CTDO_Filtrado=new ArrayList();
}

